import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        // 初始化user为对象（避免undefined）
        user: {
            token: localStorage.getItem('blog-token') || null,
            username: null
        }
    },
    mutations: {
        SET_TOKEN(state, token) {
            state.user.token = token
            localStorage.setItem('blog-token', token)
        },
        CLEAR_TOKEN(state) {
            state.user.token = null
            localStorage.removeItem('blog-token')
        },
        SET_USERNAME(state, username) {
            state.user.username = username
        }
    },
    // actions: {
    //     async login({ commit }, { username, password }) {
    //         // 模拟登录逻辑...
    //     }
    // }
})